<template>
  <div class="qxfp_box">
    <header-comm></header-comm>
    <div class="header_sele" :style="$parent.$parent.collapsed==false?'left: 10%':'left:80px'">
      <div class="add_qy"></div>
      <div>
        <div class="seach">
          <div class="sele">
            <a-input placeholder="企业ID/公司名称/手机号" style="width: 250px" @change="onSearch" />
            <a-icon type="search" class="search" />
          </div>
        </div>
      </div>
    </div>
    <!-- 表格-================= -->
    <div class="bcg">
      <div class="glwz">
        <a-table
          @change="handleChangelist"
          :loading="loading"
          :columns="columns"
          :pagination="pagination"
          :dataSource="data"
          bordered
        >
          <template v-for="col in ['roleName']" :slot="col" slot-scope="text, record, index">
            <div :key="col">
              <a-select
                v-if="record.editable"
                showSearch
                value="请选择"
                optionFilterProp="children"
                style="width: 200px"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="roleNameChange(roleName, record.key,col) "
                :filterOption="filterOption"
                v-model="roleName"
                :allowClear="true"
              >
                <a-select-option
                  v-for="(item,i) in roleList"
                  :key="i"
                  :value="item.name"
                >{{item.name}}</a-select-option>
              </a-select>
              <template v-else>{{text}}</template>
            </div>
          </template>

          <template slot="operation" slot-scope="text, record, index">
            <div class="editable-row-operations">
              <span v-if="record.editable">
                <a @click="() => save(record.key)" class="del">保存</a>
                <a-popconfirm title="确认取消吗?" class="del" @confirm="() => cancel(record.key)">
                  <a>取消</a>
                </a-popconfirm>
              </span>
              <a v-else class="del" @click="()=>xiugai(record,record.key)">权限分配</a>
              <a-popconfirm class="del" title="确定密码更新吗?" @confirm="() => resetpass(record)">
                <a href="javascript:;">密码随机更新</a>
              </a-popconfirm>
              <a-select class="del" :placeholder="record.groupName" style="width: 150px" @change="selhandleChange" :data-item="record">
                <a-select-option v-for="(item,i) in powerGroupList" :key="i" @click="groupList(record)" :value="item.GroupID">{{item.GroupName}}</a-select-option>
              </a-select>
           <a  @click="() => modfiy(record)" class="del">资料修改</a>

            </div>
          </template>
        </a-table>
        <div class="fot_z">注：注册用户会员等级为 “默认会员” 类型</div>
      </div>
    </div>
 <div class="zlxg_tk_box" v-if="saleflag">
    <sale-info></sale-info>
 </div>

  </div>
</template>

<script>
import qs from "qs";
import headerComm from "../header-comm/header.vue";
import saleInfo from './saleinfo.vue';
const columns = [
  {
    title: "企业ID",
    dataIndex: "sqdwid"
  },
  {
    title: "公司名称",
    dataIndex: "dwmc"
  },

  {
    title: "姓名",
    dataIndex: "lxr"
  },

  {
    title: "手机",
    dataIndex: "phone"
  },
  {
    title: "角色",
    dataIndex: "roleName",
    scopedSlots: { customRender: "roleName" }
  },
  {
    title: "操作",
    dataIndex: "operation",
    scopedSlots: { customRender: "operation" }
  }
];
//上传图片
function getBase64(img, callback) {
  const reader = new FileReader();
  let file = [];
  file = reader;
  reader.addEventListener("load", () => callback(reader.result));
  reader.readAsDataURL(img);
  sessionStorage.setItem("file", reader);
}
const data = [];
export default {
  data() {
    this.cacheData = data.map(item => ({ ...item }));
    return {
      data,
      columns,
      serchvalue: "",
      pagination: {
        defaultPageSize: 10,
        showTotal: total => `共 ${total} 条数据`,
        total: 0
      },
      loading: true,
      selectedRowKeys: [],
      roleName: "",
      validnum: "",
      roleList: [],
      sqlval:'',
      powerGroupList:'',
      saleflag:false,
      saleval:''
    };
  },
  components: {
    headerComm,
    saleInfo
    // xzl
  },
  methods: {
    //搜索框
    onSearch(e) {
      this.serchvalue = e.target.value;

      this.handleChangelist(this.pagination);
    },

    //表格
    handleChangelist(pagination) {
      this.axios
        .post(
          "quanxian/PowerList.php",
          qs.stringify({
            page: pagination.current,
            pageSize: pagination.defaultPageSize,
            filed: this.serchvalue
          })
        )
        .then(res => {
          if (res.data.code == 303) {
            this.data = res.data.result.data;
            this.pagination.total = Number(res.data.result.total);
            this.loading = false;
            this.cacheData = this.data.map(item => ({
              ...item
            }));
            for (var i = 0; i < res.data.result.data.length; i++) {
              this.data[i].key = i;
            }
            // this.getpowerGroupList()
          } else {
            this.data = [];
          }
        });
    },
    modfiy(value){
      console.log(value)
      this.saleval=value
      this.saleflag=true
    },
    roleNameChange(value, key, column) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.key)[0];
      if (target) {
        target[column] = value;
        this.data = newData;
      }
      this.roleName = value;
    },
    //获取角色列表
    getroleList() {
      this.axios.post("quanxian/roleList.php").then(res => {
        if (res.data.code == 303) {
          this.roleList = res.data.result.result;
        }
      });
    },
    xiugai(value, key) {
      console.log(value)

      const newData = [...this.data];
      const target = newData.filter(item => key === item.key)[0];
      if (target) {
        target.editable = true;
        this.data = newData;
      }
      this.validnum = value.ID_NUM;
      this.roleName=value.roleName
    },

    save(key) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.key)[0];
      if (target) {
        delete target.editable;
        this.data = newData;
        this.cacheData = newData.map(item => ({ ...item }));
      }
      this.axios
        .post(
          "quanxian/PowerListEdit.php",
          qs.stringify({
            id_num: this.validnum,
            rolename: this.roleName
          })
        )
        .then(res => {
          if (res.data.code == 303) {
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg
            });
            this.roleName = '';
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg
            });
            this.roleName = "";
          }
        });
    },
    //密码重置
    resetpass(value) {
      this.axios
        .post(
          "quanxian/resetPass.php",
          qs.stringify({
            id_num: value.ID_NUM
          })
        )
        .then(res => {
          if (res.data.code == 303) {
            const key = `open${Date.now()}`;
            this.$notification.open({
              message: "密码重建成功",
              description: "手机：" + value.phone + " 密码：" + res.data.result,
              btn: h => {
                return h(
                  "a-button",
                  {
                    props: {
                      type: "primary",
                      size: "small"
                    },
                    on: {
                      click: () => this.$notification.close(key)
                    }
                  },
                  "确认"
                );
              },
              key,
              onClose: close
            });
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg
            });
          }
        });
    },
    cancel(key) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.key)[0];
      if (target) {
        Object.assign(
          target,
          this.cacheData.filter(item => key === item.key)[0]
        );
        delete target.editable;
        this.data = newData;
      }
    },
    //列表
    handleBlur() {},
    handleFocus() {},
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
    //文章分类列表
    // quanxian/powerGroupList.php
    getpowerGroupList(){
      this.axios.post("quanxian/powerGroupList.php").then(res=>{
        console.log(res)
        if(res.data.code==303){
          this.powerGroupList=res.data.result
        }
      })
    },
    selhandleChange(value) {
      console.log(`selected ${value}`);
       this.axios.post("quanxian/PowerGroupEdit.php",qs.stringify({
        phone:this.sqlval.phone,
        sqdwid :this.sqlval.sqdwid,
        groupId :value
      })).then(res=>{
        console.log(res)
       if (res.data.code == 303) {
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg
            });
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg
            });
          }
      })
    },
    groupList(value){
      console.log(value)
      this.sqlval=value

     
    }
  },
  computed: {},
  created() {
    this.handleChangelist(this.pagination);
    this.getroleList();
    this.getpowerGroupList()
  }
};
</script>

<style lang='less' scoped>
.qxfp_box {
  position: relative;

  .header_sele {
    position: absolute;
    top: 55px;
    left: 200px;
    z-index: 999;
    text-align: right;
    width: 90%;
    background-color: #fff;
    box-shadow: 5px 5px 5px #ccc;
    // height: 50px;
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .seach {
      display: inline-block;
      .sele {
        position: relative;
        i {
          position: absolute;
          top: 10px;
          right: 8px;
        }
      }
    }
    .list_head {
      display: inline-block;
      .tit {
        font-size: 16px;
      }
      padding: 10px 15px;
      .fl_right {
        .margin {
          margin: 0 5px;
        }
        .xzl_btn:hover {
          border: 0 !important;
          color: #fff !important;
          background: #1890ff !important;
        }
      }
    }
  }
  .glwz {
    width: 98%;
    position: absolute;
    top: 147px;
    left: 1%;
    background-color: #fff;
    border: 1px solid #ccc;
    .ant-table-thead > tr,
    .ant-table-tbody > tr {
      height: 50px !important;
    }
    .del {
    }
  }

  .ant-spin-nested-loading > div > .ant-spin {
    top: 100px !important;
  }
  .del {
    padding-right: 15px;
    color: #40a9ff;
    cursor: pointer;
  }
  .fot_z {
    position: absolute;
    bottom: 20px;
    left: 26px;
    color: #999;
  }
  .zlxg_tk_box{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 1000px;
    background: rgba(0,0,0,.5);
    z-index: 999;
  }
}
</style>
